﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Xml;
using System.Collections;

namespace TDLib.Xml
{
    #region 类XSDHelper 用于遍历xsd,获得子节点的属性等

    /// <summary>
    ///  类XSDHelper 用于遍历xsd,获得子节点的属性等
    /// </summary>
    public class XSDHelper
    {
        /// <summary>
        /// 获得XML（xsd）文档的NameSpceManager
        /// </summary>
        /// <param name="xmlDoc">一个XML（xsd）文档</param>
        /// <returns>此XML（xsd）的NameSpceManager</returns>
        public static XmlNamespaceManager GetNSMgr(XmlDocument xmlDoc)
        {
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);

            if (nsMgr.HasNamespace("xsd") == false)
                nsMgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");

            if (nsMgr.HasNamespace("msdata") == false)
                nsMgr.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata");

            return nsMgr;
        }


        /// <summary>
        /// 获得所给XML（xsd）文档的根节点
        /// </summary>
        /// <param name="xmlDoc">文档（xsd）</param>
        /// <returns>根节点</returns>

        public static XmlNode GetXSDColumnRoot(XmlDocument xmlDoc)
        {
            return xmlDoc.DocumentElement.SelectSingleNode(".//xsd:sequence", GetNSMgr(xmlDoc));
        }

        /// <summary>
        /// 获得所给XML（xsd）文档的根节点
        /// </summary>
        /// <param name="xmlDoc">文档（xsd）</param>
        /// <param name="strRoot">根节点名称</param>
        /// <returns>根节点</returns>
        public static XmlNode GetXSDColumnRoot(XmlDocument xmlDoc, string strRoot)
        {
            return xmlDoc.SelectSingleNode(strRoot, GetNSMgr(xmlDoc));
        }

        /// <summary>
        ///从数据字典中获得一个列节点
        /// </summary>
        /// <param name="xmlDoc">数据字典</param>
        /// <param name="strColumnName">列名称</param>
        /// <returns>列节点</returns>
        public static XmlNode GetXSDColumnNode(XmlDocument xmlDoc, string strColumnName)
        {
            return xmlDoc.SelectSingleNode(
                                    ".//xsd:sequence/xsd:element[@name = \"" + strColumnName + "\"]",
                                    GetNSMgr(xmlDoc));
        }

        /// <summary>
        /// 从数据字典中获得一个列节点
        /// </summary>
        /// <param name="xmlDoc">数据字典</param>
        /// <param name="strRoot">根结点名称</param>
        /// <param name="strColumnName">列名称</param>
        /// <returns>列节点</returns>
        public static XmlNode GetXSDColumnNode(
                                                XmlDocument xmlDoc,
                                                string strRoot,
                                                string strColumnName
                                                )
        {
            return xmlDoc.SelectSingleNode(
                                    strRoot + "/xsd:element[@name = \"" + strColumnName + "\"]",
                                    GetNSMgr(xmlDoc));
        }

        /// <summary>
        /// 获得列节点（数据表的列）的属性值
        /// </summary>
        /// <param name="nodeColumn">列节点</param>
        /// <param name="strAttr">属性名</param>
        /// <returns>列节点（数据表的列）的属性值</returns>
        public static string GetXSDColumnAttr(XmlNode nodeColumn, string strAttr)
        {
            XmlAttribute xmlAttr = nodeColumn.Attributes[strAttr];

            if (xmlAttr == null)
                return string.Empty;
            else
                return xmlAttr.Value;

        }

        /// <summary>
        ///  缺省根节点字符串参数，直接获得列节点（数据表的列名）的属性值
        /// </summary>
        /// <param name="xmlDoc">数据字典</param>
        /// <param name="strColumnName">列名称</param>
        /// <param name="strAttr">列属性名称</param>
        /// <returns>属性值</returns>
        public static string GetXSDColumnAttrDirect(
                                                    XmlDocument xmlDoc,
                                                    string strColumnName,
                                                    string strAttr
                                                    )
        {
            XmlNode xmlnodeColumn = GetXSDColumnNode(xmlDoc, strColumnName);

            return GetXSDColumnAttr(xmlnodeColumn, strAttr);
        }


        /// <summary>
        /// 直接获得子节点（数据表的列名）的属性值 （提供根节点字符串参数）
        /// </summary>
        /// <param name="xmlDoc">数据字典</param>
        /// <param name="strRoot">根结点名称</param>
        /// <param name="strColumnName">列名称</param>
        /// <param name="strAttr">属性名称</param>
        /// <returns>属性值</returns>
        public static string GetXSDColumnAttrDirect(
                                                    XmlDocument xmlDoc,
                                                    string strRoot,
                                                    string strColumnName,
                                                    string strAttr
                                                    )
        {
            XmlNode xmlnodeColumn = GetXSDColumnNode(xmlDoc, strRoot, strColumnName);

            return GetXSDColumnAttr(xmlnodeColumn, strAttr);
        }

    #endregion


    }


}
